<!--
-->
<template>
    <vxe-modal v-model="visible" :height="layout.modalHeight(400)" :min-width="layout.modalMinWidth(500)" :position="{top: '60px'}" :title="title" :width="layout.modalWidth(500)" destroy-on-close resize show-footer show-maximize @close="cancelClick">
        <div v-loading="isSubmitting" style="padding: 0 40px">
            <div class="group-title">1：软件类型</div>
            <el-radio-group v-model="state.model.progType">
                <el-radio-button :value="1">单样品</el-radio-button>
                <el-radio-button :value="2">多样品</el-radio-button>
                <el-radio-button :value="3">水泥版</el-radio-button>
            </el-radio-group>
<!--            <div class="group-title">2：框架类型</div>-->
<!--            <el-radio-group v-model="state.model.versionBitness">-->
<!--                <el-radio-button v-if="getVersionBitness(64, state.options.versionBitness)" :value="64">64位</el-radio-button>-->
<!--                <el-radio-button v-if="getVersionBitness(32, state.options.versionBitness)" :value="32">32位</el-radio-button>-->
<!--            </el-radio-group>-->
            <div class="group-title">2：探测器类型</div>
            <g-dict-select v-model="state.model.xrfType" code="XrfType" value-field="name" />
            <div class="group-title">3：电脑Mac码</div>
            <el-input v-model="state.model.macCode" placeholder="mac"/>
        </div>
        <template #footer>
            <el-button @click="cancelClick">取消</el-button>
            <el-button :loading="isSubmitting" type="primary" @click="beforeSubmit">
                确定
            </el-button>
        </template>
    </vxe-modal>
</template>

<script setup>
import {ref, reactive, computed, onMounted} from "vue";
import {FormMixins} from "@/utils/FormMixins";
import * as api from "./api.js";

const {ELForm, visible, title, layout, isLoading, isSubmitting, state, setOptions, submitSuccess, cancelClick} = FormMixins();
const {$message} = window;

const Model = () => {
    return {
        versionNum: null,
        programId: null,
        versionBitness: 64,
        progType: 1,
        isRepack: true
    }
}

/**
 * 计算编译位数
 * @param value
 * @param versionBitness
 * @returns {boolean}
 */
function getVersionBitness(value, versionBitness) {
    return (value & versionBitness) === value
}

// 校验表单
function beforeSubmit() {
    if (!state.model.progType) return $message.warning("请选择软件类型")
    if (!state.model.xrfType) return $message.warning("请选择探测器类型")
    if (!state.model.macCode) return $message.warning("请填入mac码")
    download()
}

/**
 *
 * @returns {Promise<void>}
 */
async function download() {
    const body = {
        ...state.model,
    }
    isSubmitting.value = true
    const progTypes = {
        1: "单样品",
        2: "多样品",
        3: "水泥版",
    }
    const res = await api.DownloadVersionFiles(body, `${state.model.fileName}_${progTypes[body.progType]}_${body.xrfType}.zip`);
    // isSubmitting.value = false
    // if (!res.success) return
    // $downloadFile($config.baseUrl + "/" + res.data)
    cancelClick()
}

// 表单显示
async function show(config) {
    setOptions(config);
    state.model = Model();
    state.model = {...state.model, ...state.options}
    // 初始化数据
    if (getVersionBitness(32, state.options.versionBitness)) {
        state.model.versionBitness = 32
    }
    if (getVersionBitness(64, state.options.versionBitness)) {
        state.model.versionBitness = 64
    }
}

onMounted(() => {
})
defineExpose({
    show
})
</script>


<style lang="scss" scoped>
.group-title {
    padding: 20px 0 10px 0;
}
.download-framework {
    padding: 40px;
    display: flex;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;

    .action {
        width: 120px;
    }
}
</style>
